﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace ProjectX
{
    /// <summary>
    /// UploadHandler 的摘要说明
    /// </summary>
    public class UploadHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            //context.Response.Charset = "utf-8";

            HttpPostedFile file = context.Request.Files["myfile"];
            string uploadPath = HttpContext.Current.Server.MapPath(@context.Request["folder"]) + "\\uploadTemp\\";

            if (file != null)
            {
                if (file.ContentLength > 512000)
                {
                    context.Response.Write("outofsize");
                    context.Response.End();
                }
                else
                {
                    string type = file.FileName.Substring(file.FileName.LastIndexOf("."));
                    string filename = DateTime.Now.ToString("yyyyMMddHHmmssff") + type;

                    file.SaveAs(uploadPath + filename);

                    context.Response.Write(filename);
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("0");
                context.Response.End();
            }  
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}